package com.example.learning3;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity2 extends AppCompatActivity {

    TextView tv_user_name;
    TextView tv_user_password;
    EditText et_input_name;
    EditText et_input_password;
    CheckBox cb_remember;
    CheckBox cb_auto_sign_in;
    Button bt_sign_in;
    Button bt_register;
    Button bt_find_remember_password;
    Button bt_remove_log;
    SharedPreferences library;
    SharedPreferences library_remember;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
        setOnclick();
    }
    @SuppressLint("WrongConstant")
    private void initView(){
        tv_user_name=findViewById(R.id.tv_user_name);
        tv_user_password=findViewById(R.id.tv_user_password);
        et_input_name=findViewById(R.id.et_input_username);
        et_input_password=findViewById(R.id.et_input_password);
        cb_remember=findViewById(R.id.cb_remember_password);
        cb_auto_sign_in=findViewById(R.id.cb_auto_sign_in);
        bt_sign_in=findViewById(R.id.bt_sign_in);
        bt_register=findViewById(R.id.bt_register);
        bt_find_remember_password=findViewById(R.id.bt_find_remember_password);
        bt_remove_log=findViewById(R.id.bt_remove_log);
        library=getSharedPreferences("user_message", Context.MODE_APPEND);//建立用户信息库
        library_remember=getSharedPreferences("user_message_remember", Context.MODE_APPEND);//建立用户信息库
    }
    private void setOnclick(){
        bt_sign_in.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name=et_input_name.getText().toString().trim();
                String password=et_input_password.getText().toString().trim();
                if(TextUtils.isEmpty(name)||TextUtils.isEmpty(password)){//判断是否为空
                    Toast.makeText(MainActivity2.this, "请输入需要登录的账号和密码", Toast.LENGTH_SHORT).show();
                }else{
                    if(library.getString(name, "").equals("")){//判断库内是否有相同数据
                        Toast.makeText(MainActivity2.this, "用户未注册", Toast.LENGTH_SHORT).show();
                    }else{//有用户名
                        if(library.getString(name,"").equals(password)){
                            Toast.makeText(MainActivity2.this, "登录成功", Toast.LENGTH_SHORT).show();
                            if(cb_remember.isChecked()){//如果需要记住密码就保存到记忆库中
                                library_remember.edit().putString("last_name",name).apply();
                                library_remember.edit().putString("last_password",password).apply();
                                Toast.makeText(MainActivity2.this, "密码已保存", Toast.LENGTH_SHORT).show();
                            }
                            Intent intent=new Intent();
                            intent.setClass(MainActivity2.this,MainActivity.class);
                            startActivity(intent);
                            MainActivity2.this.finish();
                        }else{
                            Toast.makeText(MainActivity2.this, "密码错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
        });
        bt_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name=et_input_name.getText().toString().trim();
                String password=et_input_password.getText().toString().trim();
                if(TextUtils.isEmpty(name)||TextUtils.isEmpty(password)){//判断是否为空
                    Toast.makeText(MainActivity2.this, "请输入需要注册的账号和密码", Toast.LENGTH_SHORT).show();
                }else{
                    if(library.getString(name, "").equals("")){//判断库内是否有相同数据
                        library.edit().putString(name,password).apply();
                        Toast.makeText(MainActivity2.this, "注册成功", Toast.LENGTH_SHORT).show();
                    }else{//若无
                        Toast.makeText(MainActivity2.this, "该用户名已被注册", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        bt_find_remember_password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String last_name=library_remember.getString("last_name","");
                String last_password=library_remember.getString("last_password","");
                if(TextUtils.isEmpty(last_name)||TextUtils.isEmpty(last_password)){
                    Toast.makeText(MainActivity2.this, "无登录记录", Toast.LENGTH_SHORT).show();
                }else{
                    et_input_name.setText(last_name);
                    et_input_password.setText(last_password);
                }
            }
        });
        bt_remove_log.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("CommitPrefEdits")
            @Override
            public void onClick(View v) {
                library_remember.edit().clear().commit();
            }
        });
    }
}